package io.renren.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.service.CrudService;
import io.renren.common.utils.JacksonUtils;
import io.renren.common.utils.PageData;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * CRUD基础服务类
 *
 * @author lzy
 */
@SuppressWarnings({"PMD","AlibabaClassNamingShouldBeCamel"})
public abstract class CrudServiceImpl<M extends BaseMapper<T>, T, V> extends BaseServiceImpl<M, T>
        implements CrudService<T, V> {

    @SuppressWarnings("unchecked")
    protected Class<V> currentvoClass() {
        return getSuperClassGenericType(getClass(), 2);
    }

    @Override
    public PageData<V> page(Map<String, Object> params) {
        IPage<T> page = baseDao.selectPage(getPage(params, Constant.CREATE_DATE, false), getWrapper(params));
        return getPageData(page, currentvoClass());
    }

    @Override
    public List<V> list(Map<String, Object> params) {
        List<T> entityList = baseDao.selectList(getWrapper(params));
        return JacksonUtils.listObj2pojo(entityList, currentvoClass());
    }

    public abstract QueryWrapper<T> getWrapper(Map<String, Object> params);

    @Override
    public V get(Long id) {
        T entity = baseDao.selectById(id);
        return JacksonUtils.obj2pojo(entity, currentvoClass());
    }

    @Override
    public void delete(Long[] ids) {
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }
}
